package com.sgb.packers.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.packers.R;
import com.sgb.packers.view.adapter.TimeSalaryAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.interfaces.DialogClick;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/11/25 12:24
 * description:
 */
public class TimeSalaryPopWindow extends BasePopWindow implements View.OnClickListener {

    private DialogClick mDialogClick;
    private TimeSalaryAdapter timeSalaryAdapter;
    private TextView tvTitle;

    public void setDialogClick(DialogClick mDialogClick) {
        this.mDialogClick = mDialogClick;
    }

    public TimeSalaryPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.packers_pop_time;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView tvPopSure = holder.getPop().findViewById(R.id.tv_pop_sure);
        TextView tvPopCancel = holder.getPop().findViewById(R.id.tv_pop_cancel);
        RecyclerView rvTimeSalary = holder.getPop().findViewById(R.id.rv_time_salary);
        tvTitle = holder.getPop().findViewById(R.id.tv_title);

        tvPopCancel.setOnClickListener(this);
        tvPopSure.setOnClickListener(this);

        this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        this.setClippingEnabled(false);

        rvTimeSalary.setLayoutManager(new GridLayoutManager(mContext, 4));
        List<String> time = new ArrayList<>();
        for (float i = 1; i < 49.0f; i++) {
            time.add(String.format("%s小时", String.valueOf(i / 2).endsWith(".0") ? String.valueOf(i / 2).replace(".0", "") : String.valueOf(i / 2)));
        }
        timeSalaryAdapter = new TimeSalaryAdapter(mContext, time);
        timeSalaryAdapter.setItemSelect(true);
        rvTimeSalary.setAdapter(timeSalaryAdapter);


    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_pop_sure) {
            if (timeSalaryAdapter.getCheckDatas().size() > 0) {
                mDialogClick.confirmClick(timeSalaryAdapter.getCheckDatas().get(0), null);
            } else {
                mDialogClick.confirmClick("", null);
            }
        } else if (v.getId() == R.id.tv_pop_cancel) {
            mDialogClick.cancelClick();
            dismiss();
        }
    }

    public void setTitle(String title){
        tvTitle.setText(title);
    }

    public void setDefault(double workNumber){
        int position = 0;
        for (int i = 0; i < timeSalaryAdapter.getDatas().size(); i++) {
            if (workNumber == Double.parseDouble(timeSalaryAdapter.getDatas().get(i).replace("小时",""))){
                position = i;
            }
        }
        timeSalaryAdapter.setDefItem(position);
    }
}
